<?php
/**
 * Created by PhpStorm.
 * User: Quyet
 * Date: 12/5/2014
 * Time: 10:35 AM
 */
include '../../db/dbConnect.php';

function select_quest_user($iduser,$kind,$start,$limit){
	//select all of questions u_user ansered;
	//$limit=0 ~=unlimit => $start = don't care
	//$kind=1: about question, $kind=0: normal question
	if($kind==0){
		$sql='select q.question as ques, a.answer as ans, uqa.explain as exp, q.idpr_question as idq
			from u_profile_question as q, u_profile_answer as a, u_quest_answer as uqa
			where uqa.idquestion=q.idpr_question
			 	and uqa.idanswer=a.idpr_answer 
			 	and q.idpr_question=a.idquestion
				and q.kind="'.$kind.'"
				and uqa.iduser="'.$iduser.'"';
				if($limit && ($start>=0)){
                    $sql.='limit '.$start.','.$limit.'';
                }
	}elseif($kind==1){
		$sql='select q.question as ques, ab.content as ans
		from u_profile_question as q, u_answer_about as ab 
		where q.idpr_question=ab.idpr_question
			and iduser="'.$iduser.'" order by ab.idpr_question asc';
	}
	$result=mysql_query($sql);
	echo mysql_error();
	return $result;
} 
 
function select_macth_quest($a,$b){//lay ra cac cau hoi cua a ma b da tra loi;
	$sql='select * 
			from u_quest_answer as q
			where q.iduser="'.$a.'" 
				and q.idquestion IN 
				(select qb.idquestion from u_quest_answer as qb where qb.iduser="'.$b.'") 
				and q.idanswer IN 
				(select qb.idanswer from u_quest_answer as qb where qb.iduser="'.$b.'")
			order by idquestion asc';
	$result=mysql_query($sql);
	if(mysql_error()){
		return mysql_error();
	}
	return $result;
}
function select_sum_level($iduser){
	$sql='select sum(level) as sum_lv from u_quest_answer where iduser='.$iduser;
	echo mysql_error();
	$result=mysql_query($sql);
	$row=mysql_fetch_array($result);
	return $row['sum_lv'];
}
function select_user2pref($iduser, $idquest){
	//tim ra tat ca cac u_user thuoc đoi tuong tim kiem cua $iduser co tra loi cau hoi $idquest
	//tra lai một kết quả query danh sach cac iduser
	//find out aim
	$sql='select aim from u_user where iduser="'.$iduser.'"';
	$result=mysql_query($sql);
	$row=mysql_fetch_array($result);
	$aim=$row['aim'];
	//find out u_user list;
	$sql='select distinct qua.iduser as iduser
			from u_quest_answer as qua, u_user as u 
			where qua.iduser=u.iduser
				and qua.iduser !="'.$iduser.'"
				and qua.idquestion = "'.$idquest.'"
			';
	if($aim){		
			$sql.=' and u.sex="'.$aim.'" and u.active > 0';
	}
			$sql.=' order by u.iduser asc';
	$result=mysql_query($sql);
    echo mysql_error();
	return $result;
}

function select_user4sex($sex){echo 'select_user4sex()<br/>';
	//tim u_user theo gioi tinh
		 $sql='select distinct up.user 
				from u_pref as up, u_user
				where up.user=u_user.iduser
					and u_user.sex="'.$sex.'"
				order by up.user asc';
		$result=mysql_query($sql);
		echo mysql_error();
		return $result;	
		echo 'end select_user4sex()<br/>';
}
function select_aim_user($iduser){
	//tim ra cac user_aim cua $iduser 
		$sql='select distinct user_aim 
				from u_pref 
				where u_pref.user="'.$iduser.'"
				order by pref asc';
		$result=mysql_query($sql);
		echo  mysql_error();
		return $result;
}

function update_pref_db($iduser, $user_aim, $pref){//update pref of user-user_aim
	
	//kiem tra cap user-user_aim da co pref chua
	$sql='select *
		from u_pref
		where u_pref.user="'.$iduser.'"
			and user_aim="'.$user_aim.'"';
	$result=mysql_query($sql);
	echo mysql_error();
	$row=mysql_fetch_array($result);//update pref of user-user_aim
	$count=count($row);
	//echo "count: ".$count;print_r($row);
	//echo 'user: '.$row["user"];
	if($count>1 && $row['user']){
		//echo 'da tung ghep';
		$sql_u='update u_pref set
							pref="'.$pref.'"
				where u_pref.user="'.$iduser.'"
						and user_aim="'.$user_aim.'"	
				';
	}else{
		//echo 'tao moi cap pref';
		$sql_u='insert into u_pref(u_pref.user, user_aim, pref)
							value("'.$iduser.'","'.$user_aim.'","'.$pref.'")';
	}
	if(!mysql_query($sql_u)){
		echo mysql_error();
		return 0;
	}
	return 1;
}

function update_match_db($array){
	//print_r($array);
	foreach($array as $k=>$user){
		//print_r($user);
		//echo 'user ';
		//echo
		 $iduser=$user["iduser"];
		//echo ' aim ';
		//echo 
		$idaim=$user["status"];
//		echo 'matched ';
//		echo 
		$matched=($idaim==0)?0:1;
//		echo '<br>';
		//echo 'da tung ghep';
		$sql_u='update u_pref set
							matched="'.$matched.'"
				where u_pref.user="'.$iduser.'"
						and user_aim="'.$idaim.'"	
				';
	
		if(!mysql_query($sql_u)){
			echo mysql_error();
			return 0;
		}
		
	}return 1;	
}

?>